<?php

namespace App\Http\Controllers;

use Dingo\Api\Routing\Helpers;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use Helpers;

    protected $userInfo = [];

    public function __construct(){
        $this->userInfo = Auth::user();
    }

    /**
     * 获取成功输出
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return false|string
     */
    public function success(array $data = [], string $msg = '成功', int $code = 200)
    {
        return json_encode([
            'code'   => $code,
            'message'=> empty($msg) ? '成功' : $msg,
            'data'   => $data,
        ],JSON_UNESCAPED_UNICODE);
    }

    /**
     * 获取成功输出列表
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return false|string
     */
    public function successForList(array $data = [], string $msg = '成功', int $code = 200)
    {
        return json_encode([
            'code'   => $code,
            'message'=> empty($msg) ? '成功' : $msg,
            'data'   => $data['data'],
            'count'  => $data['count']
        ],JSON_UNESCAPED_UNICODE);
    }



    /**
     * 错误提示输出
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return JsonResponse
     */
    public function error(int $code = 40001,$data = [], string $msg = ''): JsonResponse
    {
        return response()->json([
            'code'      => $code,
            'message'   => empty($msg) ? trans("errorCode.".$code) : $msg,
            'data'      => $data
        ],200,[],JSON_UNESCAPED_UNICODE);
    }
}
